package chinatelecom.feilong.framework.web.sys.service.impl;


import chinatelecom.feilong.framework.common.jpa.DynamicSpecifications;
import chinatelecom.feilong.framework.common.jpa.SearchFilter;
import chinatelecom.feilong.framework.web.sys.dao.IDictinaryDao;
import chinatelecom.feilong.framework.web.sys.entity.Dictionary;
import chinatelecom.feilong.framework.web.sys.service.IDictionaryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by hnayan on 2017/9/4.
 */
@Transactional
@Service
public class DictionaryServiceImpl implements IDictionaryService {

    @Resource
    private IDictinaryDao iDictinaryDao;

    @Override
    public Dictionary saveOrUpdateDictionary(Dictionary dictionary) {
        return iDictinaryDao.save(dictionary);
    }


    @Override
    public Page<Dictionary> findConfigPageByParams(Map<String, Object> params, Pageable pageable, String sortName, String sortValue) {
        Page<Dictionary> configPage = null;
        if(sortName!=null&&sortValue!=null){
            Integer pageNumber = pageable.getPageNumber();
            Integer pageSize = pageable.getPageSize();
            if(sortValue.equals("asc")) {
                pageable = new PageRequest(pageNumber,pageSize, Sort.Direction.ASC, sortName);
            }else {
                pageable = new PageRequest(pageNumber,pageSize,Sort.Direction.DESC, sortName);
            }
        }
        if(params == null){
            configPage = iDictinaryDao.findAll(pageable);
        }else{
            Map<String,SearchFilter>  filters = SearchFilter.parse(params);
            Specification<Dictionary> spec = DynamicSpecifications.bySearchFilter(filters.values());
            configPage = iDictinaryDao.findAll(spec, pageable);
        }
        return configPage;
    }

    @Override
    public Dictionary findOneBdspDictionary(int id) {
        return iDictinaryDao.findOne(id);
    }

    @Override
    public Dictionary findByCode(String code) {
        return iDictinaryDao.findByCode(code);
    }

    @Override
    public void deleteDictionariesByIds(String[] ids) {
        if(ids!=null&&ids.length>0){
            for(String id:ids){
                Dictionary dictionary = iDictinaryDao.findOne(Integer.valueOf(id));
                dictionary.setIsDelete(1);
                iDictinaryDao.save(dictionary);
            }
        }
    }

    @Override
    public Dictionary saveDictionariesByCode(Dictionary dictionary, String code) {
        Dictionary old = iDictinaryDao.findByCode(code);
        //已创建就替换，还没有创建就新建
        if (old != null) {
            old.setIsDelete(0);
            old.setName(dictionary.getName());
            return iDictinaryDao.save(old);
        } else {
            dictionary.setIsDelete(0);
            return iDictinaryDao.save(dictionary);
        }

    }
    /**
     * 根据PID查找数据字典列表
     *
     * @param pId
     * @return
     */
    @Override
    public List<Dictionary> findDictionaryListByPId(Integer pId) {
        return iDictinaryDao.findDictionaryListByPId(pId);
    }

    /**
     * 根据父ID和code来查询一个字段对象
     *
     * @param pId
     * @param code
     * @return
     */
    @Override
    public Dictionary findOneDictionaryByPIdAndCode(Integer pId, String code) {
        return iDictinaryDao.findOneDictionaryByPIdAndCode(pId,code);
    }
}
